# -*- coding: utf-8 -*-

__author__	= 'Souheil CHELFOUH <souheil@chelfouh.com>'
__docformat__	= 'plaintext'

from Products.CMFCore.utils import getToolByName
from Products.PloneGossip.config import *


def installWorkflow(self, workflow, title, meta_types, isDefault=False):
    """
    Install workflow and bind them to the content types
    if isDefault is set to True, then the workflow is set as the
    portal default.
    """
    wft = getToolByName(self, 'portal_workflow')
    wft.manage_addWorkflow("%s (%s [%s])" % (workflow, title, PROJECTNAME), workflow)
    if isDefault:
        wft.setDefaultChain(workflow)
    wft.setChainForPortalTypes(meta_types, workflow)


def addToListProperty(self, out, propertySheet, property, value):
    """Add the given value to the list in the given property
    """
    current = list(propertySheet.getProperty(property))
    if value not in current:
        current.append(value)
        propertySheet.manage_changeProperties(**{property : current})
 	
        print >> out, "Added %s to %s" % (value, property)
        

def addCatalog(self, out, projectname, catalog, catalogname):
    if not hasattr(self, catalog):
        addTool = self.manage_addProduct[projectname].manage_addTool
        addTool(catalogname)
        out.write('Added %s\n' % catalogname)


def registerResources(self, out, toolname, resources):
    tool = getToolByName(self, toolname)
    existing = tool.getResourceIds()
    cook = False
    for resource in resources:
        if not resource['id'] in existing:
            # register additional resource in the specified registry...
            if toolname == "portal_css":
                tool.registerStylesheet(**resource)
            if toolname == "portal_javascripts":
                tool.registerScript(**resource)
            print >> out, "Added %s to %s." % (resource['id'], tool)
        else:
            # ...or update existing one
            parameters = tool.getResource(resource['id'])._data
            for key in [k for k in resource.keys() if k != 'id']:
                originalkey = 'original_'+key
                original = parameters.get(originalkey)
                if not original:
                    parameters[originalkey] = parameters[key]
                parameters[key] = resource[key]
                print >> out, "Updated %s in %s." % (resource['id'], tool)
                cook = True
    if cook:
        tool.cookResources()
    print >> out, "Successfuly Installed/Updated resources in %s." % tool


def resetResources(self, out, toolname, resources):
    # Revert resource customizations
    tool = getToolByName(self, toolname)
    for resource in [tool.getResource(r['id']) for r in resources]:
        for key in resource._data.keys():
            originalkey = 'original_'+key
            if resource._data.has_key(originalkey):
                try: # <- BBB
                    resource._data[key] = resource._data[originalkey]['value']
                except TypeError:
                    resource._data[key] = resource._data[originalkey]
                del resource._data[originalkey]

